<?php
class DynamicModuleAction extends BaseAction {
	
	public function index(){
		
		$searchValue = $this->_get("searchValue");
		$cid = $this->_get("cid");
		
		$catalog = CatalogHelper::getObject("catalog_id={$cid}");
		$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id=".$catalog["dynamic_module_id"]);
		$tableName = C("DB_PREFIX").$dynamicModule["table_name"];
		$catalogTable = C("DB_PREFIX")."catalog";
		
		$page = $this->_get("page");
		$page = $page>1? $page : 1;	//如page参数为[字母]或[为空]，情况的默认赋值
		$sql = "SELECT {$tableName}.*,{$catalogTable}.catalog_name FROM {$tableName} LEFT JOIN {$catalogTable} ON {$tableName}.catalog_id={$catalogTable}.catalog_id WHERE 1=1";	//分页查询SQL语句
		if($searchValue){	//关键字模糊查询
			$sql .= " AND title LIKE '%$searchValue%'";
		}
		if(!empty($catalog)){
			$str = CatalogHelper::getSubCataStr($catalog);
			if(!empty($str)){
				$arr = explode(",", $str);
				$catalogIdWhereSql = "";
				foreach ($arr as $key=>$id){
					$catalogIdWhereSql .= " OR {$tableName}.catalog_id=".$id;
					if($key == (count($arr)-1)){
						$catalogIdWhereSql .= ")";
					}
				}
				if(!empty($catalogIdWhereSql)){
					$sql .= " AND ({$tableName}.catalog_id=".$catalog["catalog_id"].$catalogIdWhereSql;
				}else{
					$sql .= " AND {$tableName}.catalog_id=".$catalog["catalog_id"];
				}
			}else{
				$sql .= " AND {$tableName}.catalog_id=".$catalog["catalog_id"];
			}
		}
		$sql .= " ORDER BY timeline DESC,id DESC";
		$pagination = CommonUtil::getPage($sql,$page,10);
		
		$this->assign("pagination",$pagination);
		$this->assign("catalog",$catalog);
		$this->assign("dynamicModule",$dynamicModule);
		$this->display();
	}
	
	//[操作流]默认页面{修改} Or {新增}
	//当传过来的ID有值，则进行修改操作流
	//当传过来的ID没值，则进行新增操作流
	public function todo(){
		
		$referUrl = $_SERVER['HTTP_REFERER'];
		$id = $this->_get("id");
		$cid = $this->_get("cid");
		
		$catalog = CatalogHelper::getObject("catalog_id=$cid");
		$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$catalog["dynamic_module_id"]}");
		$dynamicModuleFieldList = DynamicModuleFieldHelper::getList("dynamic_module_id={$catalog["dynamic_module_id"]}","order_num DESC");
		
		$sql = "SELECT * FROM ".C("DB_PREFIX").$dynamicModule["table_name"]." WHERE id={$id}";
		$obj = DynamicModuleHelper::queryObject($sql);
		if(!empty($obj)){
			foreach ($dynamicModuleFieldList as $field){
				$fieldName = $field["field_name"];
				if($field["form_type"] == FormType::$MULTI_IMAGE || $field["form_type"] == FormType::$MULTI_FILE){
					$obj[$fieldName] = CommonUtil::getPicList($obj[$fieldName]);
				}
			}
		}
		$this->assign("obj",$obj);
		$this->assign("dynamicModule",$dynamicModule);
		$this->assign("dynamicModuleFieldList",$dynamicModuleFieldList);
		$this->assign("nowTime",time());
		$this->assign("referUrl",$referUrl);
		$this->display();
	}
	
	public function add(){
		$catalogId = $this->_post("catalog_id");
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$catalog["dynamic_module_id"]}");
		$dynamicModuleFieldList = DynamicModuleFieldHelper::getList("dynamic_module_id={$catalog["dynamic_module_id"]}","order_num DESC");
		
		$sql = CommonUtil::getDynamicModuleSaveOrUpdateSQL($this->_post(), $dynamicModule, $dynamicModuleFieldList, "GetSaveSQL");
		$result = DynamicModuleHelper::updateSQL($sql);
		
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		}else{	//添加成功
			
			//只有数据库更新成功，才将文件进行真正的操作
			foreach ($dynamicModuleFieldList as $field){
				$fieldName = $field["field_name"];
				
				//上传单文件-----------------------
				if($field["form_type"] == FormType::$IMAGE || $field["form_type"] == FormType::$FILE){
					$img_text = $this->_post("{$fieldName}_text");
					CommonUtil::remove2ProductPath($img_text);
				}
				//上传多文件-----------------------
				if($field["form_type"] == FormType::$MULTI_IMAGE || $field["form_type"] == FormType::$MULTI_FILE){
					//0............初始化数据.....
					$extra_file_text_arr = $this->_post("{$fieldName}_text") == NULL ? array() : $this->_post("{$fieldName}_text");	//+添加展示图片
					$extra_file_text_arr = CommonUtil::picListBlankFilter($extra_file_text_arr);	//对$extra_pic_text_arr进行过滤，如果数组中含有空白字符的，则过滤掉
					
					//1............找出新资源文件，上传....
					$arr4upload = array();
					$i = 0;
					foreach($extra_file_text_arr as $extra_file_text){
						$arr4upload[$i] = $extra_file_text;
						$i++;
					}
					//执行上传....
					foreach($arr4upload as $file_addr4upload){
						CommonUtil::remove2ProductPath($file_addr4upload);
					}
					
				}
				
			}
			//传递给JS端，让其跳转
			echo CommonUtil::ajaxReturn(Status::$REDIRECT,null, __APP__."/center/dynamicModule/?cid={$catalogId}");
		}
	}
	
	public function update(){
		
		$id = $this->_post("id");
		$catalogId = $this->_post("catalog_id");
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$catalog["dynamic_module_id"]}");
		$dynamicModuleFieldList = DynamicModuleFieldHelper::getList("dynamic_module_id={$catalog["dynamic_module_id"]}","order_num DESC");
		$tableName = C("DB_PREFIX").$dynamicModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$obj = DynamicModuleHelper::queryObject($sql);
		if(empty($obj)){
			echo CommonUtil::ajaxReturn(Status::$ERROR,"该对象不存在，操作失败！");
			return;
		}
		$sql = CommonUtil::getDynamicModuleSaveOrUpdateSQL($this->_post(),$dynamicModule,$dynamicModuleFieldList,"GetUpdateSQL");
		$result = DynamicModuleHelper::updateSQL($sql);
		
		if($result === false){	//失败，则提示错误信息，并返回上一个页面[记录了上一个页面，用户填写的信息]
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
		}else{	//添加成功
			
			foreach ($dynamicModuleFieldList as $field){
				$fieldName = $field["field_name"];
				
				//删除前一任的文件，再进行转移临时文件-----------------------
				if($field["form_type"] == FormType::$IMAGE || $field["form_type"] == FormType::$FILE){
					$img_text = $this->_post("{$fieldName}_text");
					$imgAddr = CommonUtil::getRemovedProductPath($img_text);	//获取到缩略图正式环境下的地址
					if($obj[$fieldName] != $imgAddr){
						CommonUtil::deleteFile($obj[$fieldName]);
						CommonUtil::remove2ProductPath($img_text);
					}
				}
				//删除前一任的文件，再进行转移临时文件[批量图片上传的删除]-----------------------
				if($field["form_type"] == FormType::$MULTI_IMAGE || $field["form_type"] == FormType::$MULTI_FILE){
					//0............初始化数据.....
					$extra_file_text_arr = $this->_post("{$fieldName}_text") == NULL ? array() : $this->_post("{$fieldName}_text");	//+添加展示图片
					$extra_file_text_arr = CommonUtil::picListBlankFilter($extra_file_text_arr);	//对$extra_pic_text_arr进行过滤，如果数组中含有空白字符的，则过滤掉
					$i = 0;
					$file_list_addr = "";
					foreach ($extra_file_text_arr as $extra_file_text){	//+添加展示图片
						if($i != 0){
							$file_list_addr .= CommonType::$PIC_EXPLODE_CHAR;
						}
						$file_list_addr .= CommonUtil::getRemovedProductPath($extra_file_text);
						$i++;
					}
					
					//1............找出废弃的文件，删除.....
					$arr = CommonUtil::getPicList($obj[$fieldName]);
					$arr4delete = array();
					$i = 0;
					foreach($arr as $file_addr){
						if(!CommonUtil::contain($file_list_addr,$file_addr)){
							$arr4delete[$i] = $file_addr;
							$i++;
						}
					}
					//执行删除....
					foreach($arr4delete as $file_addr4delete){
						CommonUtil::deleteFile($file_addr4delete);
					}
					
					//2............找出新资源文件，上传....
					$arr4upload = array();
					$i = 0;
					foreach($extra_file_text_arr as $extra_file_text){
						$extra_file_addr = CommonUtil::getRemovedProductPath($extra_file_text);
						if(!CommonUtil::contain($obj[$fieldName], $extra_file_addr)){
							$arr4upload[$i] = $extra_file_text;
							$i++;
						}
					}
					//执行上传....
					foreach($arr4upload as $file_addr4upload){
						CommonUtil::remove2ProductPath($file_addr4upload);
					}
					
				}
				
			}
			echo CommonUtil::ajaxReturn(Status::$SUCCESS, "修改成功！");
		}
	}
	
	public function delete(){
		
		$catalogId = $this->_get("cid");
		$id = $this->_get("id");
		
		$catalog = CatalogHelper::getObject("catalog_id={$catalogId}");
		$dynamicModule = DynamicModuleHelper::getObject("dynamic_module_id={$catalog["dynamic_module_id"]}");
		$dynamicModuleFieldList = DynamicModuleFieldHelper::getList("dynamic_module_id={$catalog["dynamic_module_id"]}","order_num DESC");
		$tableName = C("DB_PREFIX").$dynamicModule["table_name"];
		
		$sql = "SELECT * FROM {$tableName} WHERE id={$id}";
		$obj = DynamicModuleHelper::queryObject($sql);
		if(empty($obj)){
			echo CommonUtil::ajaxReturn ( Status::$ERROR, "对象不存在，操作失败！" );
			return ;
		}
		
		$sql = "DELETE FROM {$tableName} WHERE id={$id}";
		$result = DynamicModuleHelper::updateSQL($sql);
		
		if($result === false){
			echo CommonUtil::ajaxReturn(Status::$ERROR, "服务器繁忙，请稍后重试！");
			return ;
		}
		
		//执行文件删除...........
		foreach ($dynamicModuleFieldList as $field){
			$fieldName = $field["field_name"];
			
			//单文件资源删除-----------------------
			if($field["form_type"] == FormType::$IMAGE || $field["form_type"] == FormType::$FILE){
				if(!empty($obj[$fieldName])){
					CommonUtil::deleteFile($obj[$fieldName]);
				}
			}
			//多文件资源删除-----------------------
			if($field["form_type"] == FormType::$MULTI_IMAGE || $field["form_type"] == FormType::$MULTI_FILE){
				if(!empty($obj[$fieldName])){
					$picture_list_addr = CommonUtil::getPicList($obj[$fieldName]);
					foreach ($picture_list_addr as $addr){
						CommonUtil::deleteFile($addr);
					}
				}
			}
			
		}
		
		echo CommonUtil::ajaxReturn(Status::$RELOAD_PAGE, "操作成功！");
	}
	
	
	
}